/**
 * 双指针，最差遍历数组两次
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */
var removeElement = function (nums, val) {
  let l = nums.length;
  if (l === 0) return 0;

  let slow = 0,
    fast = 0;

  while (fast < l) {
    if (nums[fast] !== val) {
      nums[slow] = nums[fast];
      slow++;
    }
    fast++;
  }

  return slow;
};

/**
 * 双指针优化，遍历数组一次
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */
var removeElement = function (nums, val) {
  let l = nums.length
  if (l === 0) return 0

  let left = 0, right = l - 1

  while (left <= right) {
      if (nums[left] === val) {
          nums[left] = nums[right]
          right--
      } else {
          left++
      }
  }

  return left
};

